package com.hydl.power.utils;

import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public class CalculateDistanceUtils {
    public static void main(String[] args){
        GlobalCoordinates source = new GlobalCoordinates(34.800571, 113.606603);
        GlobalCoordinates target = new GlobalCoordinates(34.740396, 101.622473);

        double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
        System.out.println("Sphere坐标系计算结果："+meter1 + "米");

    }


    public static Double getDistance(String startLocation, String endLocation , Ellipsoid ellipsoid){
        if(StringUtils.isNotBlank(startLocation) && StringUtils.isNotBlank(startLocation)){
            String[] startArr = startLocation.split(",");
            String[] endArr = endLocation.split(",");
            GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(startArr[1]), Double.parseDouble(startArr[0]));
            GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(endArr[1]), Double.parseDouble(endArr[0]));
            //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
            GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, source, target);
            return geoCurve.getEllipsoidalDistance();
        }
        return null;
    }


    public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){
        //创建GeodeticCalculator，调用计算方法，传入坐标系、经纬度用于计算距离
        GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

        return geoCurve.getEllipsoidalDistance();
    }
}